//
//  QuickFightViewController.m
//  namefight
//
//  Created by nasa on 11-3-29.
//  Copyright 2011年 nasawz.com. All rights reserved.
//

#import "QuickFightViewController.h"
#import "PersonIndexer.h"
@interface QuickFightViewController (Private)
- (void)displayPersonPropertyWithLabDex:(UILabel *)labDex 
                                 LabAgi:(UILabel *)labAgi 
                                 LabVit:(UILabel *)labVit 
                                LabMana:(UILabel *)labMana 
                                 LabAct:(UILabel *)labAct 
                                LabLuck:(UILabel *)labLuck 
                                   Dict:(NSDictionary *)dict;
- (void)displayP1WithName:(NSString *)name;
- (void)displayP2WithName:(NSString *)name;
@end

@implementation QuickFightViewController

int currTag;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.view setBackgroundColor:[UIColor whiteColor]];
		self.title = @"乱斗";
        
		self.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"乱斗" image:[UIImage imageNamed:@"icon_fight.png"] tag:0] autorelease];
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)loadView
{
    [super loadView];
    UIImage * img = [UIImage imageNamed:@"select_bg.png"];
    UIImageView * bgView = [[UIImageView alloc] initWithImage:img];
    [self.view addSubview:bgView];
    TT_RELEASE_SAFELY(bgView);
    TT_RELEASE_SAFELY(img);
    
    UIImage * img2 = [UIImage imageNamed:@"select_layer2_bg.png"];
                                             //select_layer2_bg
    UIImageView * bg_layer2_View = [[UIImageView alloc] initWithImage:img2];
    [self.view addSubview:bg_layer2_View];
    TT_RELEASE_SAFELY(bg_layer2_View);
    TT_RELEASE_SAFELY(img2);
    
    UIImage * img1 = [UIImage imageNamed:@"paper_buttom.png"];
    UIImageView * paper_buttom = [[UIImageView alloc] initWithImage:img1];
    paper_buttom.frame =CGRectMake(0, 360, 318, 8);
    [self.view addSubview:paper_buttom];
    TT_RELEASE_SAFELY(paper_buttom);
    TT_RELEASE_SAFELY(img1);
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    {
        p1name = [[UITextField alloc] initWithFrame:CGRectMake(8, 8, 100, 30)];
        p1name.borderStyle = UITextBorderStyleRoundedRect;
        p1name.placeholder = @"p1name";
        p1name.textAlignment = UITextAlignmentLeft;
        p1name.keyboardType = UIKeyboardTypeNamePhonePad;
        p1name.returnKeyType = UIReturnKeyDone;
        p1name.delegate = self;
        p1name.tag = 0;
        
        p2name = [[UITextField alloc] initWithFrame:CGRectMake(181, 8, 100, 30)];
        p2name.borderStyle = UITextBorderStyleRoundedRect;
        p2name.placeholder = @"p2name";
        p2name.textAlignment = UITextAlignmentLeft;
        p2name.keyboardType = UIKeyboardTypeNamePhonePad;
        p2name.returnKeyType = UIReturnKeyDone;
        p2name.delegate = self;
        p2name.tag = 1;
        
        
        p1dex = [[UILabel alloc] initWithFrame:CGRectMake(50, 51, 90, 20)];
        [p1dex setBackgroundColor:[UIColor clearColor]];
        p1dex.text = @"";
        p2dex = [[UILabel alloc] initWithFrame:CGRectMake(222, 51, 90, 20)];
        [p2dex setBackgroundColor:[UIColor clearColor]];
        p2dex.text = @"";
        
        p1agi = [[UILabel alloc] initWithFrame:CGRectMake(50, 81, 90, 20)];
        [p1agi setBackgroundColor:[UIColor clearColor]];
        p1agi.text = @"";
        p2agi = [[UILabel alloc] initWithFrame:CGRectMake(222, 81, 90, 20)];
        [p2agi setBackgroundColor:[UIColor clearColor]];
        p2agi.text = @"";
        
        p1vit = [[UILabel alloc] initWithFrame:CGRectMake(50, 111, 90, 20)];
        [p1vit setBackgroundColor:[UIColor clearColor]];
        p1vit.text = @"";
        p2vit = [[UILabel alloc] initWithFrame:CGRectMake(222, 111, 90, 20)];
        [p2vit setBackgroundColor:[UIColor clearColor]];
        p2vit.text = @"";
        
        p1mana = [[UILabel alloc] initWithFrame:CGRectMake(50, 141, 90, 20)];
        [p1mana setBackgroundColor:[UIColor clearColor]];
        p1mana.text = @"";
        p2mana = [[UILabel alloc] initWithFrame:CGRectMake(222, 141, 90, 20)];
        [p2mana setBackgroundColor:[UIColor clearColor]];
        p2mana.text = @"";
        
        p1act = [[UILabel alloc] initWithFrame:CGRectMake(50, 171, 90, 20)];
        [p1act setBackgroundColor:[UIColor clearColor]];
        p1act.text = @"";
        p2act = [[UILabel alloc] initWithFrame:CGRectMake(222, 171, 90, 20)];
        [p2act setBackgroundColor:[UIColor clearColor]];
        p2act.text = @"";
        
        p1luck = [[UILabel alloc] initWithFrame:CGRectMake(50, 201, 90, 20)];
        [p1luck setBackgroundColor:[UIColor clearColor]];
        p1luck.text = @"";
        p2luck = [[UILabel alloc] initWithFrame:CGRectMake(222, 201, 90, 20)];
        [p2luck setBackgroundColor:[UIColor clearColor]];
        p2luck.text = @"";
        
        [self.view addSubview:p1name];
        [self.view addSubview:p2name];
        
        
        [self.view addSubview:p1dex];
        [self.view addSubview:p2dex];
        
        [self.view addSubview:p1agi];
        [self.view addSubview:p2agi];
        
        [self.view addSubview:p1vit];
        [self.view addSubview:p2vit];
        
        [self.view addSubview:p1mana];
        [self.view addSubview:p2mana];
        
        [self.view addSubview:p1act];
        [self.view addSubview:p2act];
        
        [self.view addSubview:p1luck];
        [self.view addSubview:p2luck];
        
        [p1name release];
        [p1dex release];
        [p1agi release];
        [p1vit release];
        [p1mana release];
        [p1act release];
        [p1luck release];
        [p2name release];
        [p2dex release];
        [p2agi release];
        [p2vit release];
        [p2mana release];
        [p2act release];
        [p2luck release];
    }
    
    //select from address
    {
        UIButton * sadd1 = [UIButton buttonWithType:UIButtonTypeContactAdd];
        sadd1.frame = CGRectMake(113, 10, 29, 29);
        sadd1.tag = 0;
        [sadd1 addTarget:self action:@selector(fromAddress:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:sadd1];
        
        UIButton * sadd2 = [UIButton buttonWithType:UIButtonTypeContactAdd];
        sadd2.frame = CGRectMake(286, 10, 29, 29);
        sadd2.tag = 1;
        [sadd2 addTarget:self action:@selector(fromAddress:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:sadd2];
    }
    
    
    UIButton * blackground = [UIButton buttonWithType:UIButtonTypeCustom];
    blackground.frame = self.view.frame;
    [blackground addTarget:self action:@selector(blackgroundClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:blackground atIndex:0];
    
    UIButton * btnFight = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage * img_btn = [UIImage imageNamed:@"btn_fight.png"];
    [btnFight setImage:img_btn forState:UIControlStateNormal];
    TT_RELEASE_SAFELY(img_btn);
    btnFight.frame = CGRectMake(88, 312, 145, 35);
    [btnFight addTarget:self action:@selector(onFight:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnFight];
}

- (void)onFight:(id)sender {
//    NSLog(@"fight");
    if ([p1name.text length]!=0 && [p2name.text length]!=0) {
        [[PersonIndexer shareInstance] setP1:p1name.text];
        [[PersonIndexer shareInstance] setP2:p2name.text];
        TTOpenURL(@"tt://challenge");
    }
}

- (void)blackgroundClick:(id)sender {
    [p1name resignFirstResponder];
    [p2name resignFirstResponder];
}

- (void)viewDidUnload
{
    p1name = nil;
   p1dex = nil;
   p1agi = nil;
   p1vit = nil;
   p1mana = nil;
   p1act = nil;
   p1luck = nil;
   p2name = nil;
   p2dex = nil;
   p2agi = nil;
   p2vit = nil;
   p2mana = nil;
   p2act = nil;
   p2luck = nil;
    
    [ppnc release];
    ppnc = nil;
    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma -
- (void)displayP1WithName:(NSString *)name {
    [self displayPersonPropertyWithLabDex:p1dex 
                                   LabAgi:p1agi 
                                   LabVit:p1vit 
                                  LabMana:p1mana 
                                   LabAct:p1act 
                                  LabLuck:p1luck 
                                     Dict:[PersonIndexer createPersonProperty:name]];
}
- (void)displayP2WithName:(NSString *)name {
    [self displayPersonPropertyWithLabDex:p2dex 
                                   LabAgi:p2agi 
                                   LabVit:p2vit 
                                  LabMana:p2mana 
                                   LabAct:p2act 
                                  LabLuck:p2luck 
                                     Dict:[PersonIndexer createPersonProperty:name]];
}
- (void)displayPersonPropertyWithLabDex:(UILabel *)labDex 
                                 LabAgi:(UILabel *)labAgi 
                                 LabVit:(UILabel *)labVit 
                                LabMana:(UILabel *)labMana 
                                 LabAct:(UILabel *)labAct 
                                LabLuck:(UILabel *)labLuck 
                                   Dict:(NSDictionary *)dict {
    labDex.text = [NSString stringWithFormat:@"%d",[[dict objectForKey:@"dex"] intValue]];
    labAgi.text = [NSString stringWithFormat:@"%d",[[dict objectForKey:@"agi"] intValue]];
    labVit.text = [NSString stringWithFormat:@"%d",[[dict objectForKey:@"vit"] intValue]];
    labMana.text = [NSString stringWithFormat:@"%d",[[dict objectForKey:@"mana"] intValue]];
    labAct.text = [NSString stringWithFormat:@"%d",[[dict objectForKey:@"act"] intValue]];
    labLuck.text = [NSString stringWithFormat:@"%d",[[dict objectForKey:@"luck"] intValue]];
}
#pragma mark -
#pragma UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//    NSLog(@"textFieldShouldReturn tag = %d",textField.tag);
    [textField resignFirstResponder];
    if (textField.text != @"") {
        if (textField.tag == 0) {
            [self displayP1WithName:textField.text];
        }else{
            [self displayP2WithName:textField.text];
        }
    }    
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
//    NSLog(@"textFieldDidEndEditing tag = %d",textField.tag);
    if (textField.text != @"") {
        if (textField.tag == 0) {
            [self displayP1WithName:textField.text];
        }else{
            [self displayP2WithName:textField.text];
        }
    }
    //NSLog(@"textFieldDidEndEditing %@",textField.text);
    //[self createPersonProperty];
}
#pragma mark -
- (void)fromAddress:(id)sender {
    //NSLog(@"fromAddress");
    currTag = ((UIButton *)sender).tag;
    if (ppnc == nil) {
        ppnc = [[ABPeoplePickerNavigationController alloc] init];
    }
	ppnc.peoplePickerDelegate = self;
    
	[self presentModalViewController:ppnc animated:YES];
}
#pragma mark PEOPLE PICKER DELEGATE METHODS
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    if (currTag == 0) {
        p1name.text = [[ABContact contactWithRecord:person] compositeName];
        [self displayP1WithName:p1name.text];
    }else if(currTag == 1){
        p2name.text = [[ABContact contactWithRecord:person] compositeName];
        [self displayP2WithName:p2name.text];
    }
	[self dismissModalViewControllerAnimated:YES];
//	[peoplePicker release];
    //[self createPersonProperty];    
	return NO;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
	// required method that is never called in the people-only-picking
	[self dismissModalViewControllerAnimated:YES];
//	[peoplePicker release];
	return NO;
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
	[self dismissModalViewControllerAnimated:YES];
//	[peoplePicker release];
}
@end
